package com.sunsys.hdfs;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

public class Copy {

	/**
	 * As we are not using the hdfs schema while creating the FileSystem instance, 
	 * 		The dst folder will be treated as local file system folder/path if hadoop conf/site-conf.xml is not in the class path
	 * 		The dst folder will be treated as hdfs folder/path.
	 * 	
	 * @param src String 
	 * @param dst String 
	 */
	public static void copyLocalFile(String src, String dst) {
		FileSystem fileSystem = null;
		Configuration configuration = null;
		
		configuration = new Configuration();
		try {
			fileSystem = FileSystem.get(configuration);
			fileSystem.copyFromLocalFile(new Path(src), new Path(dst));
		}catch(IOException e1) {
			e1.printStackTrace();
		}
	}

	/**
	 * src file will be copied to dst. (dst path related folders gets created, if not there.  
	 * @param schema String
	 * @param src String
	 * @param dst String
	 */
	public static void copyLocalFile(String schema, String src, String dst) {
		FileSystem fileSystem = null;
		Configuration configuration = null;
		URI uri = null;
		try {
			uri = new URI(schema);
			configuration = new Configuration();
			fileSystem = FileSystem.get(uri, configuration);
			fileSystem.copyFromLocalFile(new Path(src), new Path(dst));
			
		}catch(URISyntaxException e1) {
			e1.printStackTrace();
		}catch(IOException e2) {
			e2.printStackTrace();
		}
	}
	
	public static void main(String []args) { 
 
		String src = "/home/ubuntu/Desktop/Sample1.txt";
		String dst = "/home/abc/Sample2.txt";
		String schema = "hdfs://localhost:9000/";
		Copy.copyLocalFile(schema, src, dst);
		
	}
	
}
